package org.wevil.util;

import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis缓存工具类
 *
 * @author wevil
 * @since 2023/10/19
 */
public class RedisUtil {
    private static final RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) BeanUtil.getBean("redisTemplate");
    /**
     * 获取过期时间
     *
     * @param key key
     * @return 时间(秒) 返回小于等于0代表为永久有效
     */
    public static Long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    /**
     * 判断缓存是否存在
     *
     * @param key key
     * @return true 存在 false不存在
     */
    public static Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key key
     * @return value
     */
    public static Object get(String tntId, String key) {
        return redisTemplate.opsForValue().get(String.format("%s:%s", tntId, key));
    }

    /**
     * 写入缓存
     *
     * @param key key
     * @param value value
     * @return boolean
     */
    public static boolean set(String key, Object value) {
        return set(key, value, 0);
    }

    /**
     * 写入缓存
     *
     * @param key key
     * @param value value
     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
     * @return boolean
     */
    public static boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                redisTemplate.opsForValue().set(key, value);
            }
            return true;
        } catch (Exception e) {
            System.out.println("Redis写入异常：" + e.getMessage());
            return false;
        }
    }


    /**
     * 删除缓存
     *
     * @param key key
     * @return boolean
     */
    public static Boolean del(String key) {
        try {
            return redisTemplate.delete(key);
        } catch (Exception e) {
            System.out.println("Redis操作异常：" + e.getMessage());
            return false;
        }
    }

    /**
     * 模糊删除 前缀
     *
     * @param key key
     * @return 删除数量
     */
    public static Long delByPrefix(String key) {
        try {
            Set<String> keys = redisTemplate.keys(String.format("%s%s", key, "*"));
            if (keys==null || keys.isEmpty()) {
                return 0L;
            }
            return redisTemplate.delete(keys);
        } catch (Exception e) {
            System.out.println("Redis操作异常：" + e.getMessage());
            return 0L;
        }
    }

    /**
     * 发布消息
     *
     * @author wanglei
     * @since 2023/10/19
     * */
    public static void publish(String channel, String key, Object value){
        redisTemplate.opsForList().leftPush(String.format("%s:%s", channel, key), value);
    }

    /**
     * 获取消息队列大小
     *
     * @author wanglei
     * @since 2023/10/19
     * */
    public static Long size(String channel, String key){
        return redisTemplate.opsForList().size(String.format("%s:%s", channel, key));
    }

    /**
     * 接收消息
     *
     * @author wanglei
     * @since 2023/10/19
     * */
    public static Object consume(String channel, String key){
        return redisTemplate.opsForList().rightPop(String.format("%s:%s", channel, key));
    }
}
